﻿////////////////////////////////////////////////////////////////////////////////
//
// Arp Rich Internet Application Framework (http://osflash.org/arp)
// Copyright © 2004-2006 Aral Balkan (http://flashant.org)
//
// Released under the open-source MIT license.
// (http://www.opensource.org/licenses/mit-license.php)
//
// See license.txt for full license terms.
//
////////////////////////////////////////////////////////////////////////////////

package org.osflash.arp
{
	import flash.utils.describeType;
	
	
	// import org.osflash.arp.log.Log;
	
	/**
	* <p><b>The ModelLocator provides a single location to store and 
	* do named lookup on data models.</b></p> 
	* 
	* <p><i>Copyright &copy; 2004-2006 <a href="http://flashant.org">Aral Balkan</a>.
	* Arp lives at <a href="http://osflash.org/arp">http://osflash.org/arp</a>. 
	* 
	* Released under the open-source MIT license.
	* (http://www.opensource.org/licenses/mit-license.php)
	* See license.txt for full license terms.</i></p>
	*/
	public class ModelLocator
	{
	
		//////////////////////////////////////////////////////////////////////
		//
		// Properties
		//
		//////////////////////////////////////////////////////////////////////
		private var mModels:Object;
		
		private static var inst:ModelLocator;
		
		//////////////////////////////////////////////////////////////////////
		//
		// Constructor
		//
		//////////////////////////////////////////////////////////////////////
		public function ModelLocator()
		{
			mModels = new Object();
		}
	
		/**
		* Adds a model to the list of models.
		* 
		* @param	modelName	Name of the model, for named look-up.
		* @param	modelRef	Reference to the actual data object.
		*/
		public function addModel( modelName:String, modelRef:* ):void
		{
			trace ( "ModelLocator.addModel(). Adding " + modelName + "." );
			trace ( describeType(modelRef) );
			// Log.info ( modelRef );
			mModels [ modelName ] = modelRef;
		}
		
		/**
		* 	Returns reference to model with given name. Untyped on purpose.
		* 
		* @param	modelName	The name of the model object being requested.
		* @return	Reference to the requested data object or null if it
		* 			cannot be found.
		*/
		public function getModel ( modelName:String ):*
		{
			var model:* = mModels [ modelName ];
			
			if ( undefined == model )
			{
				trace ( "ModelLocator.getModel(). Could not find model " + modelName );
				return null;
			}
			else
			{
				return model;
			}
		}
	
		/**
		* Singleton accessor method.
		* 
		* @return A reference to the ModelLocator Singleton.
		*/
		public static function getInstance():ModelLocator
		{
			if ( !inst )
			{
				inst = new ModelLocator();
			}
			return inst;
		}
		public function getModels():Object{
			return mModels;
		}
	}
}